package cn.youyeku.zsxh.ui.home;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import cn.youyeku.zsxh.R;
import cn.youyeku.zsxh.Utils.LogUtils;
import cn.youyeku.zsxh.Utils.NullUtils;
import cn.youyeku.zsxh.api.XhApi;
import cn.youyeku.zsxh.app;
import cn.youyeku.zsxh.ui.UIControl;
import cn.youyeku.zsxh.ui.activity.ABS.BaseActivity;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by youye on 17-4-11.
 */

public class RegisterActivity extends BaseActivity implements View.OnClickListener {
    /**
     * register_user_email_hint
     */
    private EditText mUsername;
    /**
     * register_user_name_hint
     */
    private EditText mEMail;
    /**
     * register_password_hint
     */
    private EditText mLoginEtPassword;
    /**
     * register_password2_hint
     */
    private EditText mLoginEtPasswordCheck;
    /**
     * register
     */
    private Button mRegBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.act_user_register);
        initView();


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        menu.add(Menu.NONE, Menu.FIRST, Menu.NONE, "登录")
                .setEnabled(true)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);


        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case Menu.FIRST:
                UIControl.showLogin(getApplicationContext());
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initView() {
        mUsername = (EditText) findViewById(R.id.username);
        mEMail = (EditText) findViewById(R.id.e_mail);
        mLoginEtPassword = (EditText) findViewById(R.id.login_et_password);
        mLoginEtPasswordCheck = (EditText) findViewById(R.id.login_et_password_check);
        mRegBtn = (Button) findViewById(R.id.regBtn);
        mRegBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.regBtn:
                register();
                break;
        }
    }

    private void register() {

        String user_name = mUsername.getText().toString();
        String email = mEMail.getText().toString();
        String passwd = mLoginEtPassword.getText().toString();
        String passwd_check = mLoginEtPasswordCheck.getText().toString();



        if (NullUtils.isArrayNotNull(new String[]{user_name, email, passwd, passwd_check})) {
            if (passwd.equals(passwd_check)) {
                XhApi.register(email, user_name, passwd, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String data = response.body().string();
                        try {
                            JSONObject object = new JSONObject(data);
                            LogUtils.w(data);
                            int code = object.getInt("code");
                            String message = object.getString("message");
                            if(code == 200){
                                UIControl.showUnActiveActivity(getApplicationContext());
                            }else{
                                app.backgroundThreadShortToast(message);
                            }



                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });
            }else{
                app.backgroundThreadShortToast("两次输入的密码不正确,请重新输入.");
            }

        }


    }
}
